class RenderWithPrimitive {
    constructor(viewer, url, style) {
        this.features = null;
        this.viewer = viewer;
        this.url = url;
        this.style = style;
        this.visibleLevel = (this.style && this.style.level) ? this.style.level : 20000;
        this.fontSize = (this.style && this.style.size) ? this.style.size : 25;
        this.fontColor = (this.style && this.style.color) ? this.style.color : Cesium.Color.RED;
        this.labels = this.viewer.scene.primitives.add(new Cesium.LabelCollection());
        this.features = InitialParameter(this.url);
        // $.get(this.url, function(data) {
        //     var josnN = typeof data == 'string' ? JSON.parse(data) : data;
        //     this.features = josnN;
        // })
        this.addPostRender();
    }

    //添加点图元
    addPoint() {

    }

    //添加场景事件
    addPostRender() {
        this.viewer.scene.postRender.addEventListener(this.postRender, this);
    }

    //场景渲染事件 实时更新标签的位置 使其与笛卡尔坐标一致
    postRender() {
        if (!this.features.length) {
            this.viewer.scene.postRender.removeEventListener(this.postRender, this); //移除事件监听
            return;
        }
        var extent = getCurrentExtent({ height: this.visibleLevel });
        var featureList = [];
        if (extent) {
            featureList = this.ergodicFeatures(extent);
        }
        for (var i = 0; i < featureList.length; i++) {
            this.labels.add({
                position: new Cesium.Cartesian3.fromDegrees(featureList[i]["X"], featureList[i]["Y"], 10),
                text: featureList[i]["NAME"],
                scale: 0.6,
                font: 'normal ' + this.fontSize + 'px MicroSoft YaHei',
                scaleByDistance: new Cesium.NearFarScalar(this.visibleLevel / 10, 1, this.visibleLevel, 0.6),
                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                fillColor: this.fontColor,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                verticalOrigin: Cesium.VerticalOrigin.CENTER,
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, this.visibleLevel),
                heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND, //CLAMP_TO_GROUND
                outlineWidth: 2,
                outlineColor: Cesium.Color.WHITE
            })
        }
    }

    //根据条件筛选需要显示的要素
    ergodicFeatures(extent) {
        var xmin = extent["xmin"];
        var xmax = extent["xmax"];
        var ymin = extent["ymin"];
        var ymax = extent["ymax"];
        var features = [];
        if (xmin && xmax && ymin && ymax) {
            for (var i = 0; i < this.features.length;) {
                var curX = this.features[i]["X"];
                var curY = this.features[i]["Y"];
                if (curX >= xmin && curX <= xmax && curY >= ymin && curY <= ymax) {
                    features.push(this.features[i]);
                    this.features.splice(i, 1);
                } else {
                    i++;
                }
            }
        } else {}
        return features;
    }

    setVisible(visible) {
        if (typeof(visible) == "undefined") {
            this.labels.show = !this.labels.show;
        } else {
            this.labels.show = visible;
        }
    }

    //关闭 
    remove() {
        this.viewer.scene.postRender.removeEventListener(this.postRender, this); //移除事件监听
        this.labels = this.viewer.scene.primitives.remove(this.labels);
    }
}